package com.ruoyi.system.domain;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.entity.PaymentCallbackMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class PaymentCallbackMessageConverter {

    private static final Logger log = LoggerFactory.getLogger(PaymentCallbackMessageConverter.class);

    public static TbOrder convertToOrder(PaymentCallbackMessage paymentCallbackMessage) {
        log.info("正在转换支付回调消息为订单,{}",paymentCallbackMessage.toString());
        TbOrder order = new TbOrder();
        try {
            order.setClearDate(DateUtil.parse(paymentCallbackMessage.getClearDate()));
            order.setMerTransDate(DateUtil.parse(paymentCallbackMessage.getMerTransDate()));
            order.setTransStatus(paymentCallbackMessage.getTransStatus().name());
            order.setActivChannel(paymentCallbackMessage.getActivChannel());
            order.setTransSeqNo(paymentCallbackMessage.getTransSeqNo());
            order.setTransAmount(paymentCallbackMessage.getTransAmount());
            order.setMerchantId(paymentCallbackMessage.getMerchantId());
            order.setOrderId(paymentCallbackMessage.getOrderId());
            order.setChannelId(paymentCallbackMessage.getChannelId());
            order.setDiscountAmt(paymentCallbackMessage.getDisCountAmt());
            try{
                if(StrUtil.isNotBlank(paymentCallbackMessage.getToken()))
                {
                    paymentCallbackMessage.setPassengerId(Long.parseLong(StpUtil.getLoginIdByToken(paymentCallbackMessage.getToken()).toString()));
                }
            }catch (Exception e)
            {
                e.printStackTrace();
            }

            order.setPassengerId(paymentCallbackMessage.getPassengerId());
            order.setPassengerTel(paymentCallbackMessage.getPassengerTel());
            order.setComplaintId(paymentCallbackMessage.getComplaintId());
            order.setEvaluateId(paymentCallbackMessage.getEvaluateId());
        } catch (Exception e) {
            log.error("PaymentCallbackMessageConverter convertToOrder error", e);
        }

        return order;
    }
}
